﻿using System.Collections.ObjectModel;
using System.ComponentModel;
using MVVM.Models;
using MVVM.Commands;
using System.Windows.Input;

namespace MVVM.ViewModels
{
    public class Names : INotifyPropertyChanged
    {
        public string NameForValidation { get; set; }
        public NamesModel NamesModel { get; private set; }

        public ObservableCollection<string> NamesList
        {
            get
            {
                return this.NamesModel.Names;
            }
        }
        private ICommand addCommand;

        public ICommand Add
        {
            get
            {
                if (this.addCommand == null)
                {
                    this.addCommand = new RelayCommand(
                        () =>
                        {
                            this.AddName("a");
                        });
                }

                return this.addCommand;
            }
        }

        public ICommand AddCustomCommand { get; private set; }

        public Names()
        {
            this.NamesModel = new NamesModel();
            this.AddCustomCommand = new CustomCommand(this);
        }

        public void AddName(string name)
        {
            this.NamesModel.AddName(name);
            OnPropertyChanged("NamesList");
        }

        public bool DeleteName(string name)
        {
            bool deleteResult = this.NamesModel.DeleteName(name);
            OnPropertyChanged("NamesList");

            return deleteResult;
        }

        public event PropertyChangedEventHandler PropertyChanged;

        private void OnPropertyChanged(string propertyName)
        {
            if (this.PropertyChanged != null)
            {
                this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }
}
